package com.oracle.lingshangejia.utils;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.oracle.lingshangejia.activity.LoginActivity;

import org.json.JSONObject;

import java.util.Map;


/**
 * Created by Sl on 2019/3/1.  连接网络
 */

public class OkHttpUtils {

    /**
     * 连接网络
     * @param context  上下文
     * @param url   连接
     * @param params  参数
     * @param handler  handler
     */
    public static void getNetWork(Context context, String url, Map<Object,Object> params, final Handler handler) {
        if(HttpUtils.isNetworkConnected(context)){   //本地有网络
            String token = (String) SpUtils.get(context,"token", "");
            int mark= (int) SpUtils.get(context,"mark",0);
            params.put("token",token);
            params.put("mark",mark);
            JSONObject jsonObject = new JSONObject(params);
            MyLogger.i(jsonObject.toString());
            OkGo.<String>post(url)
                    .tag(context)
                    .upJson(jsonObject.toString())
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            Log.i("Insert requestSuccess", ""+response.code()+response.body());
                            if(response.body().contains("Token已过期")){
                                Toast.makeText(context,"登录过期，请重新登录",Toast.LENGTH_SHORT).show();
                                context.startActivity(new Intent(context, LoginActivity.class));
                                SpUtils.put(context,"isLogin",false);
                                AppManagerDelegate.getInstance().finishAllActivity();

                            }else{
                                Message msg = handler.obtainMessage();
                                msg.obj = response.body();
                                msg.what = 1;
                                handler.sendMessage(msg);
                            }

                        }

                        @Override
                        public void onError(Response<String> response) {
                            super.onError(response);
                            Message msg = handler.obtainMessage();
                            if(response.code()!=0){  //连接服务器失败
                                msg.obj = "连接服务器失败";
                                msg.what = 4;
                            }else{           //连接服务器正常报的错误
                                msg.obj = response.message();
                                msg.what = 2;
                            }
                            handler.sendMessage(msg);
                        }
                    });

        }else{
            Message msg = handler.obtainMessage();
            msg.obj = "网络不可用";
            msg.what = 3;
            handler.sendMessage(msg);
        }
    }

    /**
     * 连接网络
     * @param context  上下文
     * @param url   连接
     * @param params  参数
     * @param handler  handler
     */
    public static void getNetLoginWork(Context context, String url, Map<String,Object> params, final Handler handler) {
        if(HttpUtils.isNetworkConnected(context)){   //本地有网络
//            String token = (String) SpUtils.get(context,"token", "");
//            params.put("token",token);
            JSONObject jsonObject = new JSONObject(params);
            Log.e("json",jsonObject.toString());
            OkGo.<String>post(url)
                    .tag(context)
                    .upJson(jsonObject.toString())
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            Log.e("Insert requestSuccess", ""+ response.body());
                            Message msg = handler.obtainMessage();
                            msg.obj = response.body();
                            msg.what = 1;
                            handler.sendMessage(msg);
                        }

                        @Override
                        public void onError(Response<String> response) {
                            super.onError(response);
                            Message msg = handler.obtainMessage();
                            if(response.code()!=0){  //连接服务器失败
                                msg.obj = "连接服务器失败";
                                msg.what = 4;
                            }else{           //连接服务器正常报的错误
                                msg.obj = response.message();
                                msg.what = 2;
                            }
                            handler.sendMessage(msg);
                        }
                    });

        }else{
            Message msg = handler.obtainMessage();
            msg.obj = "网络不可用";
            msg.what = 3;
            handler.sendMessage(msg);
        }
    }
}
